package com.ucs.ui;

import java.util.List;

import com.ucs.domain.Category;
import com.ucs.domain.Item;
import com.ucs.util.UCSData;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class CategoryItemListAdaptor extends BaseExpandableListAdapter {
	private List<Category> categories = UCSData.getInstance().getCategories();
	
	@Override 
	public Object getChild(int groupPosition, int childPosition) {
		return categories.get(groupPosition).getItems().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(
			int groupPosition, 
			int childPosition, 
			boolean isLastChild,  
			View convertView, 
			ViewGroup parentView) {            
		TextView textView = getGenericView(parentView);
		Item item = (Item)getChild(groupPosition, childPosition);
		textView.setText(item.getName1());            
		return textView;        
	}        

	@Override
	public int getChildrenCount(int groupPosition) {
		return categories.get(groupPosition).getItems().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return categories.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return categories.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parentView) {
		TextView textView = getGenericView(parentView);
		Category category = (Category)getGroup(groupPosition);
		textView.setText(category.getName1());            
		return textView;        
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
	public static TextView getGenericView(View parentView) { 
		return getGenericView(parentView, 44, 36);
	}        
	public static TextView getGenericView(View parentView, int height, int padding) {            
		// Layout parameters for the ExpandableListView            
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, height);            
		TextView textView = new TextView(parentView.getContext());            
		textView.setLayoutParams(lp);           
		// Center the text vertically            
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);            
		// Set the text starting position            
		textView.setPadding(padding, 0, 0, 0);            
		return textView;        
	}        


}
